//Export UBB Frame
function couple_ExportUbbFrame() {

	$("#txaArticle").removeAttr("disabled");

	var sUbbFrame="";

	sUbbFrame=sUbbFrame+("<div id=\"UbbLabel\" style=\"display:none;\"><div onclick=\"$('#UbbFrame').hide().addClass('ubb-frame-hide');\" id=\"UbbFrame\" class=\"ubb-frame-hide\" style=\"display:none;position:absolute;z-index:540;width:520px;margin-top:20px;\"></div><span>");
	sUbbFrame=sUbbFrame+("<a onmousedown=\"if($('#UbbFrame').attr('class')=='ubb-frame-hide'){$('#UbbLabel').hide();}else{$('#UbbFrame').hide().addClass('ubb-frame-hide');}\" style=\"float:right;cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/close.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("<a onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[B]','[/B]'),true);\" style=\"cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/text_bold.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("<a onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[I]','[/I]'),true);\" style=\"cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/text_italic.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("<a onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[U]','[/U]'),true);\" style=\"cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/text_underline.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("<a onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[S]','[/S]'),true);\" style=\"cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/text_strikethrough.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("<a onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[URL]','[/URL]'),true);\" style=\"cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/link.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("<a onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[URL=http://]','[/URL]'),true);\" style=\"cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/link_add.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("<a onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[EMAIL]','[/EMAIL]'),true);\" style=\"cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/email_add.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("<a onmousedown=\"InsertText(objActive,ReplaceText(objActive,'[QUOTE]','[/QUOTE]'),true);\" style=\"cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/comment.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("<a onclick=\"if($('#UbbFrame').attr('class')=='ubb-frame-hide'){couple_InsertUbbFace();$('#UbbFrame').fadeIn('normal').removeClass('ubb-frame-hide');}else{$('#UbbFrame').fadeOut('normal').addClass('ubb-frame-hide');}\" style=\"cursor:pointer;\"><img src=\""+str00+"themes/"+strThemeName+"/style/default/emoticon_smile.png\"/></a>  ");
	sUbbFrame=sUbbFrame+("</span>");
	sUbbFrame=sUbbFrame+("</div>");

	$("#txaArticle").before(sUbbFrame);

	$("#txaArticle").focus(function(){$("#UbbLabel").fadeIn("normal");})

}
function couple_InsertUbbFace() {

		var aryFileName="";
		var strFileName="";
		var strFaceHtml="";

		aryFileName = strFaceName.split("|");

		for (var i=0;i<aryFileName.length;i++)
		{
			strFileName = aryFileName[i];
			strFaceHtml=strFaceHtml + "<img src=\""+str00+"image/face/"+strFileName+".gif\" title=\""+strFileName+"\" alt=\""+strFileName+"\" width=\""+strFaceSize+"\" height=\""+strFaceSize+"\" onclick=\"InsertText(objActive,'[F]'+this.alt+'[/F]',false);\" style=\"padding:2px;cursor:pointer;\" />";
		}

		$("#UbbFrame").html(strFaceHtml);

}
$(document).ready(function(){couple_ExportUbbFrame();});


//Revert Comment
function couple_RevertComment(intRevID) {
	var f=document.getElementById(objActive).form;

	if (f.action.search("act=CommentRev")>0){
		f.action=f.action.replace(/act=CommentRev&id=[0-9]+/g,"act=cmt");
	}

	f.action=f.action.replace("cmd.asp?act=cmt","cmd.asp?act=CommentRev&id="+intRevID);

	//hide input
	var $obj=$("#divCommentPost");
	var $objT=$("#comment"+intRevID);
	var sName=$objT.children("li.msggravatar").children("a:last").html();

	$obj.find("#inpEmail").parent("p").hide();
	$obj.find("#inpHomePage").parent("p").hide();

	//Move Form
	$obj.insertAfter($objT);
	$("#divAjaxComment").insertBefore("#comment"+intRevID);

	//Cancle Button
	$obj.children("div.post-top").children("p.cancle-revert").remove();
	$obj.children("div.post-top").children("p").children("a").html(langRevComments.replace("%m",sName));
	$obj.children("div.post-top").prepend("<p class=\"cancle-revert\"><a href=\"javascript:void(0)\" onclick=\"cancelRevert()\"><span>["+langCancleRev+"]</span></a></p>");

	//Form Effect
	$obj.hide().fadeIn("slow");

	return true;

}
function cancelRevert() {
	var f=document.getElementById(objActive).form;

	if (f.action.search("act=CommentRev")>0){
		f.action=f.action.replace(/act=CommentRev&id=[0-9]+/g,"act=cmt");
	}

	//Move Form
	$("#divCommentPost").insertAfter("ul.cmt:last");
	$("#divAjaxComment").insertAfter("ul.cmt:last");
	$("div.pagebar",$("#pageContent")).insertAfter("#divAjaxComment");

	//Set val
	$("#divCommentPost>div.post-top>p.cancle-revert").remove();
	$("#divCommentPost>div.post-top>p>a").html(langPostComments);

	//show input, check if info is hidden
	if ($("#frmSumbit>p:first")[0].className!=="welcome-back"){
		$("#inpEmail").parent("p").show();
		$("#inpHomePage").parent("p").show();
	}
}


//ajax comment
function couple_VerifyMessage() {

	var strName=document.getElementById("inpName").value;
	var strEmail=document.getElementById("inpEmail").value;
	var strHomePage=document.getElementById("inpHomePage").value;
	var strArticle;
	

	if(document.getElementById("txaArticle").value){
		strArticle=document.getElementById("txaArticle").value;
	}
	else{
		strArticle=document.getElementById("txaArticle").innerText;
	}

	if(strName==""){
		alert(str01);
		return false;
	}
	else{
		re = new RegExp("^[.A-Za-z0-9\u4e00-\u9fa5]+$");
		if (!re.test(strName)){
			alert(str02);
			return false;
		}
	}

	if(strEmail==""){
		//alert(str01);
		//return false;
	}
	else{
		re = new RegExp("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$");
		if (!re.test(strEmail)){
			alert(str02);
			return false;
		}
	}

	if(typeof(strArticle)=="undefined"){
		alert(str03);
		return false;
	}

	if(typeof(strArticle)=="string"){
		if(strArticle==""){
			alert(str03);
			return false;
		}
		if(strArticle.length>intMaxLen)
		{
			alert(str03);
			return false;
		}
	}

	document.getElementById("inpArticle").value=strArticle;
	document.getElementById("inpLocation").value=parent.window.location.href;

	//get origin comment id, if new comment return 0.
	var intRevID;
	if(document.getElementById("frmSumbit").action.search("act=CommentRev")>0){
		intRevID=document.getElementById("frmSumbit").action.match(/id=[0-9]+/i).join().replace("id=","");
	}else{
		intRevID=0;
	}


	//ajax comment begin
	$("#CommentPosting").css("display","inline");
	$("#CommentPosting").html(iconAjaxLoadingTiny+langPostingComments);
	$("#frmSumbit:submit").attr("disabled","disabled");

	//disable mng
	var $objCmtMng=$("a.commentRev,a.commentQuote,p.cancle-revert,#txaClear",$("#commentsArea"));
	$objCmtMng.hide();


	$.post(document.getElementById("frmSumbit").action,
		{
		"inpAjax":true,
		"inpID":$("#inpId").val(),
		"inpVerify":(document.getElementById("inpVerify")?$("#inpVerify").val():""),
		"inpEmail":strEmail,
		"inpName":strName,
		"inpArticle":strArticle,
		"inpHomePage":strHomePage
		},
		function(data){
			var s=data;
			var $objCmtContent;
			if((s.search("faultCode")>0)&&(s.search("faultString")>0))
			{
				alert(s.match("<string>.+?</string>")[0].replace("<string>","").replace("</string>",""))
			}
			else{
				$("#txaArticle").val("");
				$("#comment"+intRevID).remove();
				$(s).insertBefore("#divAjaxComment");
				$objCmtContent=$("#divAjaxComment").prev("ul.cmt");
				$objCmtContent.hide();
				$objCmtMng.show();

				var bolRemember=document.getElementById("chkRemember").checked;
				if(bolRemember==true){couple_SaveRememberInfo();}else{couple_SetCookie("chkRemember",bolRemember,365);}

				//Counter Again
				countChar();
				//cancel Revert
				cancelRevert();
				//Load Remember Info
				couple_LoadRememberInfo();

				//comment Align
				commentAlignAlt();
				//none cmt url
				noneCommentUrl(intRevID);
				//Show mng links
				showMng(intRevID);

				//Show New Comment
				$objCmtContent.fadeIn(2800);

				//Height
				if(!IE6){alignHeight();}

				//Save mark via cookies
				if (intRevID==0){
					saveCommentMark("#"+$("ul.cmt:last",$("#commentsArea")).attr("id"));
					showCommentMark();
				}

			}

			if(document.getElementById("inpVerify")){
				$("#inpVerify").val("");
				var objImageValid=$("img[@src^='"+str00+"function/c_validcode.asp?name=commentvalid']");
				objImageValid.attr("src",str00+"function/c_validcode.asp?name=commentvalid"+"&random="+Math.random());
			}

			$("#frmSumbit :submit").attr("disabled","");
			$("#CommentPosting").html("");
			$("#CommentPosting").hide();

		}
	);

	return false;
	//ajax comment end

}



//Save Info
function couple_SaveRememberInfo() {

	var strName=document.getElementById("inpName").value;
	var strEmail=document.getElementById("inpEmail").value;
	var strHomePage=document.getElementById("inpHomePage").value;
	var bolRemember=document.getElementById("chkRemember").checked;


	couple_SetCookie("inpName",strName,365);
	couple_SetCookie("inpEmail",strEmail,365);
	couple_SetCookie("inpHomePage",strHomePage,365);
	couple_SetCookie("chkRemember",bolRemember,365);

}

//load Info
function couple_LoadRememberInfo() {

	var strName=GetCookie("inpName");
	var strEmail=GetCookie("inpEmail");
	var strHomePage=GetCookie("inpHomePage");
	var bolRemember=GetCookie("chkRemember");

	if(bolRemember=="true"){

		if(strName){document.getElementById("inpName").value=strName;};
		if(strEmail){document.getElementById("inpEmail").value=strEmail;};
		if(strHomePage){document.getElementById("inpHomePage").value=strHomePage;};
		if(bolRemember){document.getElementById("chkRemember").checked=bolRemember;};

	}

	if(GetCookie("username")){
		strName=unescape(GetCookie("username"))
		document.getElementById("inpName").value=strName;
	}

	//new feature
	if(bolRemember=="true"){
		if ($("#frmSumbit>p:first")[0].className!=="welcome-back"){
			$("#frmSumbit").prepend("<p class=\"welcome-back\"> "+langShowWelcomInfo.replace("%m",strName)+" <a href=\"javascript:void(0)\" onclick=\"showRememberInfo()\">"+langShowRememberInfo+"</a></p>");
		}
		//hide input
		$("#inpName").parent("p").hide();
		$("#inpEmail").parent("p").hide();
		$("#inpHomePage").parent("p").hide();
	}
}

//show Info
function showRememberInfo(){
	//show input
	$("#frmSumbit>p:eq(0)").remove();
	$("#inpName").parent("p").fadeIn("normal");
	//check if reverting
	if ($("#divCommentPost>div.post-top>p:first")[0].className!=="cancle-revert"){
		$("#inpEmail").parent("p").fadeIn("normal");
		$("#inpHomePage").parent("p").fadeIn("normal");
	}
}


//Comment Ajax Del
function loadCommentDel(iCmtID,iLogID){
	var objItem = $("#comment"+iCmtID);
	var objContent = $("#comment"+iCmtID+">li.msgbody>div.msgarticle");
	var objCmtMng = $("#comment"+iCmtID+">li.msgbody>div.msginfo");
	var objCmtDel = $("#comment"+iCmtID+">li.msgbody>div.msginfo>span>a.commentDel");

	objContent.html("<p>"+iconAjaxLoading+langDelingContent+"</p>");
	objCmtMng.css("text-decoration","line-through");
	objCmtDel.attr("href","javascript:void(0)");

	pasIntTimeOut();

	$.ajax({
		type: 'GET', dataType: 'html', timeout: intAjaxTimeOut,
		url: str00+"cmd.asp?act=CommentDel&id="+iCmtID+"&log_id="+iLogID,
		error: function(){
			objContent.html("<p>"+iconAjaxDecline+"<a href=\"javascript:loadCommentDel("+iCmtID+","+iLogID+")\">"+langErrorThenRetry+"</a></p>");
			objCmtMng.css("text-decoration","none");
			objCmtDel.attr("href","javascript:loadCommentDel("+iCmtID+","+iLogID+")");
		},
		success: function(data){
			if (data.indexOf(strSuccess)>0){
				objContent.html(iconAjaxAccept+langDelingDone);
				objContent.css("text-decoration","line-through");
				objItem.fadeOut("slow",function(){objItem.remove();commentAlignAlt();});
			}else{
				if (data.indexOf(strNoneRight)>0){
					objContent.html(iconAjaxDecline+langDelingNoneRight);
				}else{
					if(data.indexOf(strError)>0){
						objContent.html(iconAjaxDecline+langDelingError);
					}else{
						objContent.html(iconAjaxDecline+langDelingAbnormal);
					}
				}
				objContent.css("color","red");
				objCmtMng.css("text-decoration","none");
			}
		}
	});

	setTimeout(function(){
			if (objContent.html().indexOf(langDelingContent)>0){
				objContent.html("<p>"+iconAjaxLoading+langStillWorking+"</p>");
			}
		},parseInt(intAjaxTimeOut/2)
	);
}

//Trackback Ajax Del
function loadTrackBackDel(iTbID,iLogID){
	var objItem = $("#trackback"+iTbID);
	var objContent = $("#trackback"+iTbID+">li.msgbody>div.msgarticle");
	var objTbMng = $("#trackback"+iTbID+">li.msgbody>div.msginfo");
	var objTbDel = $("#trackback"+iTbID+">li.msgbody>div.msginfo>span.msginfo-right>a.trackbackDel");

	objContent.html("<p>"+iconAjaxLoading+langDelingContent+"</p>");
	objTbMng.css("text-decoration","line-through");
	objTbDel.attr("href","javascript:void(0)");

	pasIntTimeOut();

	$.ajax({
		type: 'GET', dataType: 'html', timeout: intAjaxTimeOut,
		url: str00+"cmd.asp?act=TrackBackDel&id="+iTbID+"&log_id="+iLogID,
		error: function(){
			objContent.html("<p>"+iconAjaxDecline+"<a href=\"javascript:loadTrackBackDel("+iTbID+","+iLogID+")\">"+langErrorThenRetry+"</a></p>");
			objTbMng.css("text-decoration","none");
			objTbDel.attr("href","javascript:loadTrackBackDel("+iTbID+","+iLogID+")");
		},
		success: function(data){
			if (data.indexOf(strSuccess)>0){
				objContent.html(iconAjaxAccept+langDelingDone);
				objContent.css("text-decoration","line-through");
				objItem.fadeOut("slow",function(){objItem.remove();commentAlignAlt();});
			}else{
				if (data.indexOf(strNoneRight)>0){
					objContent.html(iconAjaxDecline+langDelingNoneRight);
				}else{
					if(data.indexOf(strError)>0){
						objContent.html(iconAjaxDecline+langDelingError);
					}else{
						objContent.html(iconAjaxDecline+langDelingAbnormal);
					}
				}
				objContent.css("color","red");
				objCmtMng.css("text-decoration","none");
			}
		}
	});

	setTimeout(function(){
			if (objContent.html().indexOf(langDelingContent)>0){
				objContent.html("<p>"+iconAjaxLoading+langStillWorking+"</p>");
			}
		},parseInt(intAjaxTimeOut/2)
	);
}


//save mark via cookies
function saveCommentMark(iCmtID){
	var sIdList=GetCookie("Couple_cmtMark");
	if (sIdList==null){
		sIdList=iCmtID;
	}else{
		sIdList=sIdList+","+iCmtID;
	}
	couple_SetCookieAlt("Couple_cmtMark",sIdList,60);
}
//show mark via cookies
function showCommentMark(){
	var sIdList=GetCookie("Couple_cmtMark");
	if (sIdList!==null){
		$(sIdList).each(function(){
			if ($(this).find("blockquote").size()==0){
				$(this).children("li.msgbody").append("<blockquote class=\"self-modify\">"+iconAjaxAccept+"<a href=\"javascript:void(0)\" onclick=\"showCommentModify("+$(this).attr("id").replace("comment","")+")\">"+langSelfModifyHint+"</blockquote>");
			}
		});
	}
}
//show modify form
function showCommentModify(iCmtID){
	if ($("#comment"+iCmtID+">li.msgbody>form").size()==0){
		$("#comment"+iCmtID+">li.msgbody>p.self-modify").remove();
		$("#comment"+iCmtID+">li.msgbody").append("<p class=\"self-modify\">"+iconAjaxLoading+langLoadingData+"</p>");
		$("#comment"+iCmtID+">li.msgbody>blockquote.self-modify").hide();
		$("#comment"+iCmtID+">li.msgbody>div.msgarticle").hide();
		pasIntTimeOut();
		var sdate = new Date();
		$.ajax({
			type: 'GET', dataType: 'html', timeout: intAjaxTimeOut, cache: false,
			url: str00+"themes/"+strThemeName+"/plugin/modify.asp?act=get&id="+iCmtID+"&time="+sdate.getTime(),
			error: function(){
				$("#comment"+iCmtID+">li.msgbody>p.self-modify").html(iconAjaxDecline+"<a href=\"javascript:void(0)\" onclick=\"showCommentModify("+iCmtID+")\">"+langErrorThenRetry+"</a> <a href=\"javascript:void(0)\" onclick=\"cancleCommentModify("+iCmtID+")\">"+langErrorThenGiveup+"</a>")
			},
			success: function(data){
				var sForm="<form target=\"_self\" method=\"post\" action=\"\"><input type=\"hidden\" name=\"inpID\" value=\""+iCmtID+"\"/><textarea name=\"inpArticle\" onkeyup=\"return commentModifyHotKey("+iCmtID+",event);\">"+data+"</textarea><br/><input type=\"submit\" value=\""+langConfirm+"\" onclick=\"return postCommentModify("+iCmtID+")\"/><input type=\"button\" value=\""+langCancel+"\" onclick=\"cancleCommentModify("+iCmtID+")\"/> <span></span></form>";
				$("#comment"+iCmtID+">li.msgbody>div.msgarticle").after(sForm);
				$("#comment"+iCmtID+">li.msgbody>p.self-modify").hide();
			}
		});

		setTimeout(function(){
				if ($("#comment"+iCmtID+">li.msgbody>p.self-modify").size()>0){
					if ($("#comment"+iCmtID+">li.msgbody>p.self-modify").html().indexOf(langLoadingData)>0){
						$("#comment"+iCmtID+">li.msgbody>p.self-modify").html(iconAjaxLoading+langStillWorking);
					}
				}
			},parseInt(intAjaxTimeOut/2)
		);
	}
}
//Post modify form
function postCommentModify(iCmtID){
	$("#comment"+iCmtID+">li.msgbody>form span").html(iconAjaxLoadingTiny+langPostingComments);
	$("#comment"+iCmtID+">li.msgbody>form input:first").attr("disabled","disabled");
	$("#comment"+iCmtID+">li.msgbody>form input:last").hide();
	$.post(str00+"themes/"+strThemeName+"/plugin/modify.asp?act=post",
		{
		"inpID":iCmtID,
		"inpArticle":$("#comment"+iCmtID+" textarea").val()
		},
		function(data){
			var s=data;
			var $objCmtContent;
			if((s.search("faultCode")>0)&&(s.search("faultString")>0)){
				alert(s.match("<string>.+?</string>")[0].replace("<string>","").replace("</string>",""))
			}
			else{
				$("#comment"+iCmtID+">li.msgbody>form").remove();
				$("#comment"+iCmtID+">li.msgbody>div.msgarticle").html(s).show();
				$("#comment"+iCmtID+">li.msgbody>blockquote.self-modify").show();
			}
			$("#comment"+iCmtID+">li.msgbody>form span").html("");
			$("#comment"+iCmtID+">li.msgbody>form input:first").removeAttr("disabled");
			$("#comment"+iCmtID+">li.msgbody>form input:last").show();
		}
	);
	return false;
}
//cancel modify form
function cancleCommentModify(iCmtID){
	$("#comment"+iCmtID+">li.msgbody>form").remove();
	$("#comment"+iCmtID+">li.msgbody>p.self-modify").remove();
	$("#comment"+iCmtID+">li.msgbody>div.msgarticle").show();
	$("#comment"+iCmtID+">li.msgbody>blockquote.self-modify").show();
}
$(document).ready(function(){showCommentMark();});


// textarea counter
function countChar(){
	var objTextarea=document.getElementById("txaArticle");
	var objCounter=document.getElementById("txaCounter");
	var objClear=document.getElementById("txaClear");
	var counterValue=parseInt(intMaxLen) - objTextarea.value.length;
	objCounter.innerHTML = counterValue;

	if (counterValue > 0){
		objCounter.className = "counterPos";
	}else{
		objCounter.className = "counterNeg";
	}

	if ((counterValue-parseInt(intMaxLen)) < 0){
		objClear.innerHTML="<a href=\"javascript:void(0)\" onclick=\"txaClear();\">["+langClearTxa+"]</a>";
	}else{
		objClear.innerHTML="";
	}
}

// textarea clear
function txaClear(){
	document.getElementById("txaArticle").value="";
	countChar();
}

// Ctrl+Enter Post
function commentPostHotKey(){
	if (document.getElementById("txaArticle"))
	{
		document.getElementById("txaArticle").onkeydown = function (moz_ev) {
			var ev = null;
			if (window.event){
				ev = window.event;
			}
			else{
				ev = moz_ev;
			}
			if (ev != null && ev.ctrlKey && ev.keyCode == 13) {
				document.getElementById("submit").click();
				//return couple_VerifyMessage();
			}
		}
	}
}
function commentModifyHotKey(iCmtID,event){
	var ev = null;
	if (window.event){ev = window.event;}else{ev = event;}
	if (ev != null && ev.ctrlKey && ev.keyCode == 13) {
		return postCommentModify(iCmtID);
	}
}
$(document).ready(function(){commentPostHotKey();});

//none url
function noneCommentUrl(iCmtID){
	var $obj=null;
	switch (iCmtID){
	case -1:
		$obj=$("li.msggravatar>a");
		break;
	case 0:
		$obj=$("li.msggravatar:last>a");
		break;
	default:
		$obj=$("#comment"+iCmtID+">li.msggravatar>a");
		break;
	}
	if ($obj.size()>0){
		$obj.each(function(){
			$(this).attr("href").replace(/[^=]+=/g,"")==""?$(this).attr("title",langNoHomePage).removeAttr("href"):null;
		});
	}
}
$(document).ready(function(){noneCommentUrl(-1);});
